const crypto = require('crypto')

// 生成公钥和私钥
const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
    // 模长,默认值 2048,数值越大，加密越强，性能越低
    modulusLength: 2048,
})

// 加密
const data = 'hello world'
const encrypted = crypto.publicEncrypt(publicKey, Buffer.from(data))
console.log("加密结果" + encrypted.toString("hex"))

// 解密
const decrypted = crypto.privateDecrypt(privateKey, encrypted)
console.log("解密结果" + decrypted.toString())